<?php

/**
 * Template for rendering one
 * answer.
 *
 * Need a way to add "Accept" link
 * IF viewer is also question owner
 * We can easily do this with javascript
 * by checking viewer ID in meta
 * and comparing to owner-id
 * of question. But it's not so
 * easy on server side because we loop
 * over mongo cursor, it does not know
 * who is a viewer, so...
 * We can pass callback function but
 * right now we don't have a good way
 * to pass callback to ::loop method
 * and even if we do, we would need to apply
 * the callback to each iteration, not very
 * effecient.
 * 
 */
class tplAnswer extends StudyBuddy\Template\Template
{

	protected static function func(&$a){
		if(array_key_exists('a_edited', $a)){
			$aEdited = end($a['a_edited']);
			$aEdited['edited'] = $a['edited'];
			
			$a['edits'] = \tplEditedby::parse($aEdited, false);
		}

		if(!empty($a['i_del_ts'])){
			$a['deleted'] = ' deleted';
		}

		if(!empty($a['a_deleted'])){
			$a['deletedby'] = \tplDeletedby::parse($a['a_deleted'], false);
		}

		if(!empty($a['a_comments'])){
			/**
			 * Closure function
			 * to pass resource_id 
			 * and author id of this
			 * Answer to the tplComments
			 * This way we don't have to store
			 * duplicate data in each comment
			 * element and still be able to
			 * have access to these 2 important
			 * fields in the tplComments template
			 * We going to need id or resource owner
			 * in order to add it to the "reply" link
			 * in the form of class uid-$uid
			 * 
			 */
			$rid = $a['_id'];
			$uid = $a['i_uid'];
			$reply   = $a['reply'];
			$reply_t = $a['reply_t'];
			
			$f = function(&$data) use ($rid, $uid, $reply, $reply_t){
				$data['resource_id'] 	= $rid;
				$data['owner_id'] 		= $uid;
				$data['reply']       	= $reply;
				$data['reply_t']     	= $reply_t;
			};
			
			$a['comments_html'] = tplComment::loop($a['a_comments'], true, $f);
		}
	}

	protected static $vars = array(
	'_id' => '', // 1
	'b' => '', // 2
	'ulink' => '', // 3
	'avtr' => '', // 4
	'hts' => '', // 5
	'i_votes' => '', // 6
	'i_uid' => '0', // 7 // answer author id
	'accepted_text' => '', //8
	'accepted_class' => 'accept_answer', // 9
	'vote_up' => "\xE2\x87\xA7", // 10 \xE2\x87\xA7
	'vote_down' => "\xE2\x87\xA9", //11
	'accept_link' => '&nbsp', // 12,
	'accepted' => '', //13
	'i_flags' => '', // 14
	'edits' => '', // 15
	'deleted' => '', //16
	'deletedby' => '', //17
	'comments_html' => '', //18
	'edit_delete' => '', // 19
	'i_comments' => '0', // 20
	'nocomments' => '', //21
	'i_lm_ts' => '0', // 22
	'add_comment' => 'add comment' //23
	);


	protected static $tpl = '<table class="ans_table%16$s" id="ans%1$s" StudyBuddy:rid="%1$s" StudyBuddy:i_votes="%6$s" StudyBuddy:i_lm_ts="%22$s" StudyBuddy:i_comments="%20$s" StudyBuddy:i_uid="%7$s">
	<tr>
		<td class="td_votes" width="60px">
		<div class="votebtns cb" id="vote%1$s">
		<a id="upvote-%1$s"
			title="I like this answer (click again to cancel)"
			class="ttt ajax vote thumbup" href="/ansvote/%1$s/up" rel="nofollow">%10$s</a>
		<div id="score%1$s" class="qscore">%6$s</div>

		<a id="downvote-%1$s"
			title="I dont like this answer (click again to cancel)"
			class="ttt ajax vote thumbdown down" href="/ansvote/%1$s/down" rel="nofollow">%11$s</a>
		</div>
		<div class="acceptit anstype">%13$s</div>
		<div class="acceptit">%12$s</div>
		</td>

		<td class="td_answer">
		<div class="ans_body" id="ansbody-%1$s">%2$s</div>

		<div class="answer controls uid-%7$s" id="res_%1$s">
		   	<span class="ico flag ttt ajax" title="Flag this answer">flag</span>%19$s
		</div>
		<!-- // -->
		<table class="foot">
            <tr>
            <td class="edits" valign="top">
            %15$s
            </td>
            <td class="td_poster">
            <div class="usr_info2">
            <div class="qtime">answered <span title="%5$s" class="ts">%5$s</span></div>
            <div class="avtr32">
             <img src="%4$s" height="32" width="32" alt="">
            </div>
            	<div class="usr_details usr usr_%7$s">
            	%3$s<br>
            	<span class="reputation" title="reputation score"></span>
				</div>
			</div>
			%17$s
            </td>
            </tr>
            </table>
		</td>
	</tr>
	<tr>
	<td></td>
	<td>
		<div class="comments%21$s i_comments_%20$s" id="comments-%1$s">
		%18$s
			<div class="add_com cb fl">
				<span class="ico comment fl"> </span><a href="#" class="ajax com_link uid-%7$s" id="comlink_%1$s">%23$s</a>
			</div>
		</div>
	</td>
	</tr>
	</table>';
}
